package com.heima.ai.tool;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.heima.ai.domain.query.CourseQuery;
import com.heima.ai.pojo.Course;
import com.heima.ai.pojo.CourseReservation;
import com.heima.ai.pojo.School;
import com.heima.ai.service.ICourseReservationService;
import com.heima.ai.service.ICourseService;
import com.heima.ai.service.ISchoolService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
@RequiredArgsConstructor
public class CourseTool {

    private final ICourseService courseService;
    private final ICourseReservationService courseReservationService;
    private final ISchoolService schoolService;

    @Tool(description = "根据条件筛选和查询课程")
    public List<Course> queryCourse(@ToolParam(required = false, description = "课程查询条件") CourseQuery courseQuery) {
        QueryChainWrapper<Course> wrapper = courseService.query();
        wrapper
                .eq(StringUtils.isNotEmpty(courseQuery.getType()), "type", courseQuery.getType())//type = ?
                .le(StringUtils.isNotEmpty(courseQuery.getEdu()), "edu", courseQuery.getEdu());//edu > ?
        if (!CollectionUtils.isEmpty(courseQuery.getSorts())) {
            for (CourseQuery.Sort sort : courseQuery.getSorts()) {
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        return wrapper.list();
    }

    @Tool(description = "查询校区列表")
    public List<School> querySchool() {
        return schoolService.list();
    }

    @Tool(description = "新增试听预约单")
    public void saveCourseReservation(
            @ToolParam(description = "课程名称") String course,
            @ToolParam(description = "学生姓名") String studentName,
            @ToolParam(description = "联系方式") String contactInfo,
            @ToolParam(description = "预约校区") String school,
            @ToolParam(description = "备注") String remark
    ) {
        CourseReservation courseReservation = new CourseReservation();
        courseReservation.setCourse(course);
        courseReservation.setStudentName(studentName);
        courseReservation.setContactInfo(contactInfo);
        courseReservation.setSchool(school);
        courseReservation.setRemark(remark);
        courseReservationService.save(courseReservation);
    }
}